<?php
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $type = !empty($match[2]) ? $match[2] : "";
        $appRegisterId = !empty($match[4]) ? $match[4] : 0;
        $refCode = !empty($match[6]) ? $match[6] : 0;
        
        
        /* 
        * Kiểm tra user có tồn tại không
        * Không tồn tại thì trả về 0
        */
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["username"] = !empty($user["username"]) ? $user["username"] : "";
        $result["appRegisterId"] = $appRegisterId;
        $result["type"] = $type;    
        
        return $result;
    }
    
    function InsertSmsLogApp($data)
    {
        $sql = "INSERT INTO c_sms_log SET 
            `userId`='" . mysql_escape_string($data["userId"]) . "'
            , `partnerId`=" . mysql_escape_string($data["partnerId"]) . "
            , `type`=" . mysql_escape_string($data["type"]) . "
            , `dataId`=" . mysql_escape_string($data["id"]) . "
            , `content`='" . mysql_escape_string($data["content"]) . "'
            , `output`='" . mysql_escape_string($data["output"]) . "'
            , `sender`='" . mysql_escape_string($data["sender"]) . "'
            , `telco`='" . mysql_escape_string($data["telco"]) . "'
            , `serviceCode`=" . mysql_escape_string($data["serviceCode"]) . "
            , `price`=" . mysql_escape_string($data["price"]) . "
            , `status`=" . mysql_escape_string($data["status"]) . "
            , `transactionId`=" . mysql_escape_string($data["transactionId"]) . "
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
            , `create_date`=" . time() . "            
        ";        
        
        $result = @mysql_query($sql);
        return $result;    
    }
    
    function InsertSmsLogAppTest($data)
    {
        $sql = "INSERT INTO c_sms_log_test SET 
            `userId`='" . mysql_escape_string($data["userId"]) . "'
            , `partnerId`=" . mysql_escape_string($data["partnerId"]) . "
            , `type`=" . mysql_escape_string($data["type"]) . "
            , `dataId`=" . mysql_escape_string($data["id"]) . "
            , `content`='" . mysql_escape_string($data["content"]) . "'
            , `output`='" . mysql_escape_string($data["output"]) . "'
            , `sender`='" . mysql_escape_string($data["sender"]) . "'
            , `telco`='" . mysql_escape_string($data["telco"]) . "'
            , `serviceCode`=" . mysql_escape_string($data["serviceCode"]) . "
            , `price`=" . mysql_escape_string($data["price"]) . "
            , `status`=" . mysql_escape_string($data["status"]) . "
            , `transactionId`=" . mysql_escape_string($data["transactionId"]) . "
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
            , `create_date`=" . time() . "            
        ";        
        
        $result = @mysql_query($sql);
        return $result;    
    }
    
    function checkAppType($appId)
    {
        $sql = "SELECT os_type FROM c_application_register WHERE id=" . $appId;        
        $result = @mysql_query($sql);
        $rows = mysql_fetch_assoc($result);
        return $rows;  
    }
    
    /*
    * processing
    */
    
    function processing($data)
    {        
        $regx = "/^" . $data["syntax_code_db"] . "(\s+)(5)(\s+)(\d+)(\s+)([a-zA-Z0-9]{1,})$/si";        
//        return $regx;
        if(preg_match($regx, $data["content"])){
            
            $syntax = buildSyntax($data["content"], $regx);
            
            $data["type"] = $syntax["type"];
            $data["userId"] = $syntax["userId"];
            $data["username"] = $syntax["username"];
            $data["appRegisterId"] = $syntax["appRegisterId"];            
            
            $list_service_code = array("6022", $data["service_code_db"]);
            
            if(!empty($data["appRegisterId"]) && !empty($data["type"])){
                if(in_array($data["serviceCode"], $list_service_code)){
                    $data["status"] = 1; 
                    
                    /*
                    * check xem loại app gì j2me hoặc android hoặc ios
                    */
                    $checkAppType = checkAppType($data["appRegisterId"]);
                    
                    if(!empty($data["username"])){
                        $msg = "Truy cap kho game, ung dung, video 18+: http://" . $data["username"] . ".cucdinh.mobi";
                    } else {
                        $msg = "Truy cap kho game, ung dung, video 18+: http://giaitrihot.cucdinh.mobi";
                    }
                    
                    $response["status"] = 1;
                    if(!empty($checkAppType) && $checkAppType["os_type"]=="1"){
                        $response["msg"] = "Ban da tai thanh cong. " . $msg;
                    } else {
                        $response["msg"] = "Da kich hoat thanh cong. " . $msg;
                    }
                } else {
                    $response["status"] = 3;                        
                    $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                    
                    $data["type"] = 0;
                    $data["userId"] = 0;                
                    $data["status"] = 0;                 
                }   
            } else {
                $response["status"] = 2;                
                $response["msg"] = "Tin nhan sai cu phap";    
                
                $data["type"] = 0;
                $data["userId"] = 0;                
                $data["status"] = 0;
            }
                               
                
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["type"] = 0;
            $data["userId"] = 0;                
            $data["status"] = 0;      
        }
        
        /* log sms */
        $data["transactionId"] = $data["transactionId"];
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 1; //ID partner HDC            
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];    
        $data["id"] = 0;        
        
        $result = InsertSmsLogAppTest($data);   
        return $response;
    }
?>